package com.example.tao.smartcar.bluetooth;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.example.tao.smartcar.BaseActivity;
import com.example.tao.smartcar.R;
import com.example.tao.smartcar.utils.BluetoothService;
import com.example.tao.smartcar.view.indicator.HalfIndicatorView;
import com.example.tao.smartcar.view.indicator.IndicatorView;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class GravityControlActivity extends BaseActivity {

    private static String TAG = "GravityControlActivity";

    private SensorManager mSensormanager = null;
    private Sensor mSensor = null;
    private SensorEventListener mListener = null;

    private IndicatorView speedIndicatorView, energyIndicatorView;
    private HalfIndicatorView directionIndicatorView;

    private BluetoothService mBluetoothService = null;

    private float mAx, mAy;

    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gravity_control);

        initView();

        mBluetoothService = OperationSettingActivity.getBluetoorhService();

        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                Integer wr = Integer.valueOf(msg.what);
                mBluetoothService.write(wr.byteValue());
            }
        };

        mSensormanager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        mSensor = mSensormanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        mListener = new SensorEventListener() {
            @Override
            public void onSensorChanged(SensorEvent sensorEvent) {
                mAx = sensorEvent.values[0];
                mAy = sensorEvent.values[1];

                sendValueAndUpdateUI();
            }

            @Override
            public void onAccuracyChanged(Sensor sensor, int i) {

            }
        };
    }

    private void sendValueAndUpdateUI() {
        final byte speed, direction;

        if (mAx > -0.5 && mAx < 0.5) {
            mAx = 0;
        }

        if (Math.abs(mAx) <= 5) {
            speed = (byte) (- mAx * 8);
        } else if (mAx > 5) {
            speed = -40;
        } else {
            speed = 40;
        }
        if (Math.abs(mAy) <= 5) {
            direction = (byte) (mAy * 8);
        } else if (mAy > 5) {
            direction = 40;
        } else {
            direction = -40;
        }

        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(1);
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                Message speed_command = mHandler.obtainMessage();
                speed_command.what = 0x7F;
                mHandler.sendMessage(speed_command);
                Message speed_data = mHandler.obtainMessage();
                speed_data.what = speed;
                mHandler.sendMessage(speed_data);
                Message direction_command = mHandler.obtainMessage();
                direction_command.what = 0x7E;
                mHandler.sendMessage(direction_command);
                Message direction_data = mHandler.obtainMessage();
                direction_data.what = direction;
                mHandler.sendMessage(direction_data);
                Log.d(TAG, "Send speed:" + speed_data.what);
                Log.d(TAG, "Send direction:" + direction_data.what);
                try {
                    Thread.sleep(200);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        });

        speedIndicatorView.setValue(speed);
        directionIndicatorView.setValue(direction);
    }

    private void initView() {
        speedIndicatorView = (IndicatorView) this.findViewById(R.id.speed_indicator);
        energyIndicatorView = (IndicatorView) this.findViewById(R.id.energy_indicator);
        directionIndicatorView = (HalfIndicatorView) this.findViewById(R.id.direction_indicator);

        speedIndicatorView.setMax(40);
        speedIndicatorView.setMin(-40);
        speedIndicatorView.setValue(0);

        energyIndicatorView.setMax(100);
        energyIndicatorView.setMin(0);
        energyIndicatorView.setValue(90);

        directionIndicatorView.setMax(40);
        directionIndicatorView.setMin(-40);
        directionIndicatorView.setValue(0);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mSensormanager.unregisterListener(mListener);

        mAx = 0;
        mAy = 0;

        sendValueAndUpdateUI();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mSensormanager.registerListener(mListener, mSensor, SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBluetoothService != null) {
            mBluetoothService.stop();
        }
    }
}
